package www.houdoudou.com.customer;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import com.houdoudou.com.owner.OwnerManagement;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import www.houdoudou.com.common.CommonFunction;
import www.houdoudou.com.common.CommonMathFunction;
import www.houdoudou.com.common.ConnFactory;
import www.houdoudou.com.common.TelnumVerifyManagement;

public class CustomerManagement {

	 public static JSONObject  manage(String jsonData){
  	   
   	  JSONObject result = new JSONObject();
   		JSONObject jsonfrom= JSONObject.fromObject(jsonData);				
   		//判断请求的功能d
   		int FunctionCode=jsonfrom.getInt("FunctionCode");
   		   PreparedStatement pstm=null; 
   		   Statement  stm=null;
   		   ResultSet rs=null; 	
   		   String CurrentConnUser=CommonMathFunction.genRandomNum(10);
   		   Connection conn=ConnFactory.getCurrentConnection(CurrentConnUser);
   		   switch(FunctionCode){
   		
   		   
   		   
   		   //修改
           case 2:{
			
			// step2 数据库输入。
			 
			try {	// step2 
				   String Customer_Telnum=jsonfrom.getString("Customer_Telnum");					  
				   String sql = "select * from Tb_Customer where Customer_Telnum="+Customer_Telnum+"";   
					stm=conn.createStatement(ResultSet.TYPE_FORWARD_ONLY,ResultSet.CONCUR_UPDATABLE);
					rs= stm.executeQuery(sql);
					if(rs.next()){		
						 JSONObject innerJSONOVerify=jsonfrom;						 
						 innerJSONOVerify.put("Telnum", jsonfrom.getString("Customer_Telnum"));
						if( TelnumVerifyManagement.checkVerifyCode(innerJSONOVerify.toString())){
							rs.updateString("Customer_Secret", jsonfrom.getString("Customer_Secret"));	
							rs.updateRow();
							result.put("ActionStatus", true);
							result.put("ErrorInfo", "修改密码成功");
						}else{
							result.put("ActionStatus", false);
							result.put("ErrorInfo", "验证码不正确");
						}
						
					}else{
						result.put("ActionStatus", false);
						result.put("ErrorInfo", "用户不存在");
					}
			}catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				result.put("ActionStatus", false);
				result.put("ErrorInfo", "数据库异常");
			}finally{
				ConnFactory.closeConn(CurrentConnUser, pstm, stm, rs);
			}
          break; }//
   		   //新增
   		  case 4:{
			   try {
			 		 
					// step2 
				   String Customer_Telnum=jsonfrom.getString("Customer_Telnum");
				  
				   String sql = "select * from Tb_Customer where Customer_Telnum="+Customer_Telnum+"";   
				   stm=conn.createStatement(ResultSet.TYPE_FORWARD_ONLY,ResultSet.CONCUR_READ_ONLY);
				   System.out.println(sql);
				   rs= stm.executeQuery(sql);
				   if(!rs.next()){
			        // step3 判断验证码
					   String Verify_Code=jsonfrom.getString("Verify_Code");
					   boolean isVerifyCodeRight=false;
					   JSONObject innerJSONOVerify=jsonfrom;
					   innerJSONOVerify.put("FunctionCode", 5);
					   innerJSONOVerify.put("Telnum", Customer_Telnum);
				       JSONObject innerJSONOVerifyB= TelnumVerifyManagement.manage(innerJSONOVerify.toString());
			           if(!innerJSONOVerifyB.getBoolean("ActionStatus")){
			        	   isVerifyCodeRight=false;
			           }else{
			        	   JSONArray Verify_Codes=innerJSONOVerifyB.getJSONArray("Verify_Codes");
			        	   for(int i=0;i<Verify_Codes.size();i++){
			        		   String Corrent_Verify_Code=Verify_Codes.getJSONObject(i).getString("Verify_Code");
			        		   if(Corrent_Verify_Code.equals(Verify_Code)){
			        			   isVerifyCodeRight=true;
			        			   //删除废弃验证码
			        			   JSONObject innerJSONOVerifyDel=innerJSONOVerify;
			        			   innerJSONOVerifyDel.put("FunctionCode", 3);
			        			   TelnumVerifyManagement.manage(innerJSONOVerifyDel.toString());
			        		   }
			        	   }
			           }
			          if(isVerifyCodeRight){
			        	  sql = "insert  into Tb_Customer (Customer_Telnum,Customer_Secret) values (?,?)";
			              	pstm=conn.prepareStatement(sql);
							pstm.setString(1, jsonfrom.getString("Customer_Telnum"));
							pstm.setString(2, jsonfrom.getString("Customer_Secret"));						
						    pstm.executeUpdate();						    
						    result.put("ActionStatus", true);
							result.put("ErrorInfo", "注册成功");
			          }else{
			        	  result.put("ActionStatus", false);
						  result.put("ErrorInfo", "验证码不正确");  
			          }
				
			   
				
				}else{
					   result.put("ActionStatus", false);
						result.put("ErrorInfo", "用户已经存在");
				}
					   
			  }catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
					result.put("ActionStatus", false);
					result.put("ErrorInfo", "查询失败");
				}finally{
					ConnFactory.closeConn(CurrentConnUser, pstm, stm, rs);
				}
			   
		                 break;} //end of case 4
   		  
   		   }// end of switch functionCode
   		   
   		   
   		   return result; 
	 }
	
	 
	 public static  JSONObject  loginCheck(String jsonData){
		 // step1 所有数据定义
		   JSONObject result= new JSONObject();
		   JSONObject jsonfrom= JSONObject.fromObject(jsonData);
		   PreparedStatement pstm=null; 
		   Statement  stm=null;
		   ResultSet rs=null; 		 
		   String CurrentConnUser=CommonMathFunction.genRandomNum(10);
   		   Connection conn=ConnFactory.getCurrentConnection(CurrentConnUser);
		 
		   try {
			// step2 
			   String Customer_Telnum=jsonfrom.getString("Customer_Telnum");
			   String Customer_Secret=jsonfrom.getString("Customer_Secret");
			   String sql = "select * from Tb_Customer where Customer_Telnum="+Customer_Telnum+"";   
			   stm=conn.createStatement(ResultSet.TYPE_FORWARD_ONLY,ResultSet.CONCUR_READ_ONLY);
			   System.out.println(sql);
			   rs= stm.executeQuery(sql);
			if(rs.next()){
				String correctSecret= rs.getString("Customer_Secret");
				Double Customer_Money=rs.getDouble("Customer_Money");
				if(correctSecret.equals(Customer_Secret)){
				    	result.put("ActionStatus", true);
						result.put("ErrorInfo", "登陆成功");
						result.put("Customer_Money",Customer_Money);				
				}else{
					result.put("ActionStatus", false);
					result.put("ErrorInfo", "密码不正确");
				}
			}else{
				result.put("ActionStatus", false);
				result.put("ErrorInfo", "不存在该用户");
			}
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			result.put("ActionStatus", false);
			result.put("ErrorInfo", "数据库异常");
		}finally{
			ConnFactory.closeConn(CurrentConnUser, pstm, stm, rs);
		} 
		   
		
		   return result;
	 }
	 
}
